package com.bycx.system.service.impl;
import java.io.Serializable;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.bycx.core.exception.BusinessException;
import com.bycx.core.service.BaseService;
import com.bycx.core.util.BeanUtil;
import com.bycx.core.util.SessionUtil;
import com.bycx.core.util.StringUtils;
import com.bycx.system.dto.CodeGroupDTO;
import com.bycx.system.mapper.ASysCodeGroupMapper;
import com.bycx.system.model.ASysCodeGroup;
import com.bycx.system.model.ASysCodeInfo;
import com.bycx.system.service.IASysCodeGroupService;

@Service
public class ASysCodeGroupServiceImpl  implements IASysCodeGroupService{
	@Autowired
	private ASysCodeGroupMapper aSysCodeGroupMapper;
	
	@Override
	public ASysCodeGroup get(Map<String,Object> map){
		return aSysCodeGroupMapper.get(map);
	}
	@Override
	public List<ASysCodeGroup> getGroupDistinct(Map<String,Object> map){
		return aSysCodeGroupMapper.getGroupDistinct(map);
	}
	@Override
	public List<ASysCodeGroup> getList(Map<String,Object> map){
		return aSysCodeGroupMapper.getList(map);
	}
	
	
	/**
	 * 获取码表组中包含的码表值
	 */
	@Override
	public List<ASysCodeInfo> getGrpExistCodeLst(Map<String,Object> param ) {
		List<ASysCodeInfo> lst = aSysCodeGroupMapper.getGrpExistCodeLst(param);
		return lst;
	}
	
	/**
	 * 获取码表组没有被关联的码表值
	 */
	public List<ASysCodeInfo> getGrpNotExistCodeLst(Map<String,Object> param) {
		List<ASysCodeInfo> lst = aSysCodeGroupMapper.getGrpNotExistCodeLst(param);
		return lst;
	}
	
	/**
	 * 保存码组和码值的关系
	 */
	@Override
	public void saveGrpCodes(ASysCodeGroup sysCodeGroup, List<ASysCodeInfo> sysCodeInfos) {
		if(sysCodeGroup == null) throw new BusinessException("码组信息不存在");
		if(sysCodeInfos==null || sysCodeInfos.size() ==0) throw new BusinessException("请选择码值");
		//先删除码组基本信息的那条记录
		Map<String,Object> param = new HashMap<String,Object>();
		param.put("typeCode", sysCodeGroup.getTypeCode());
		param.put("groupCode", sysCodeGroup.getGroupCode());
		aSysCodeGroupMapper.deleteGrpBasicInfo(param);
		//保存新码组和码值的关系
		for(ASysCodeInfo sysCodeInfo : sysCodeInfos){
			sysCodeGroup.setId(StringUtils.getUUID());
			sysCodeGroup.setInstDate(new Date());
			sysCodeGroup.setValCode(sysCodeInfo.getValCode());
			aSysCodeGroupMapper.insert(sysCodeGroup);
		}
	}
	
	/**
	 * 保存或者修改码组的基本信息
	 */
	@Override
	public void saveGroup(String typeCode,String groupCode,String remark) {
		Map<String,Object> param = new HashMap<String,Object>();
		param.put("typeCode", typeCode);
		param.put("groupCode", groupCode);
		List<ASysCodeGroup> list = aSysCodeGroupMapper.getGrpBasicInfo(param);
		if(list==null || list.size()==0){ //无此码组，应该保存
			ASysCodeGroup grp = new ASysCodeGroup();
			grp.setId(StringUtils.getUUID());
			grp.setGroupCode(groupCode);
			grp.setInstDate(new Date());
			grp.setInstUserNo(SessionUtil.getCurrentASysUser().getLoginName());
			grp.setRemark(remark);
			grp.setTypeCode(typeCode);
			grp.setValCode(null);
			aSysCodeGroupMapper.insert(grp);
			return;
		}
		//有数据，应该更新且只能更新备注
		param.put("remark", remark);
		param.put("updtDate", new Date());
		aSysCodeGroupMapper.updateByTypeCodeWithGroupCode(param);
	}
	@Override
	public void setBaseMapper() {
		// TODO Auto-generated method stub
		
	}
	@Override
	public ASysCodeGroup getByPrimaryKey(Serializable primaryKey) {
		// TODO Auto-generated method stub
		return null;
	}
	@Override
	public ASysCodeGroup get(String propName, String value) {
		// TODO Auto-generated method stub
		return null;
	}
	@Override
	public List<ASysCodeGroup> getList(String propName, String value) {
		// TODO Auto-generated method stub
		return null;
	}
	@Override
	public void save(ASysCodeGroup t) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void updateByPrimaryKey(ASysCodeGroup t) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void deleteByPrimaryKey(Serializable primaryKey) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void delete(List<String> ids) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void deleteObj(List<ASysCodeGroup> list) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void sava(CodeGroupDTO codeGroupBO) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void delete(List<String> groupCodes, String typeCode) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void deleteGrpCodes(List<String> valCodes, String typeCode,
			String groupCode) {
		// TODO Auto-generated method stub
		
	}
	
}

